package com.robooot.business_counter.util;




import com.robooot.business_counter.exception.BizException;
import com.robooot.business_counter.exception.DemoError;
import com.robooot.business_counter.model.BillInfoCache;
import com.robooot.business_counter.model.BillQueryCache;

import java.math.BigDecimal;
import java.util.Map;

public class PayUtil {

    public static long yuan2fen(BigDecimal yuan) {
        if (yuan == null) {
            throw new BizException(DemoError.SYSTEM_ERROR);
        }
        return yuan.multiply(new BigDecimal(100)).intValue();
    }

    public static BigDecimal fen2yuan(long fen) {
        if (fen < 0) {
            throw new BizException(DemoError.SYSTEM_ERROR);
        }
        return new BigDecimal(fen).divide(new BigDecimal(100), 2, BigDecimal.ROUND_DOWN);
    }

    public static BillInfoCache createInfoCache(String deviceId, Map data) {
        BillInfoCache cache = new BillInfoCache();
        cache.setDeviceId(deviceId);
        if (data.get("sequence") != null) {
            cache.setSequence((Long) data.get("sequence"));
        }
        if (data.get("extra") != null) {
            cache.setExtra((String) data.get("extra"));
        }
        return cache;
    }

    public static BillQueryCache createQueryCache(int status, String tradeNo, String userId) {
        BillQueryCache cache = new BillQueryCache();
        cache.setStatus(status);
        cache.setTradeNo(tradeNo);
        cache.setUserId(userId);
        return cache;
    }

}
